package com.itheima.d5_regex;

/**
 * 目标：掌握正则表达式的书写规则
 *
 * String中可以使用正则表达式的方法学过的有哪些：
 *      public boolean matches(String regex)
 *      public String replaceAll(String regex, String replacement) 替换
 *      public String[] split(String regex)
 */
public class RegexTest2 {
    public static void main(String[] args) {
        // 1、字符类(只能匹配单个字符)
        System.out.println("\"a\".matches(\"[a-zA-Z_0-9]\") = " + "a".matches("[a-zA-Z_0-9]")); //true


        // 2、预定义字符(只能匹配单个字符)  .  \d  \D   \s  \S  \w  \W
        System.out.println("\"a\".matches(\"\\\\d\") = " + "a".matches("\\d")); //false
        System.out.println("\"1\".matches(\"\\\\d\") = " + "1".matches("\\d")); //true

        // \转义
        System.out.println("\""); //"
        // \n \t
        System.out.println("\n"); //换行
        System.out.println("\t"); //制表

        // 3、数量词： ?   *   +   {n}   {n, }  {n, m}
        System.out.println("\"123123\".matches(\"\\\\d{5,19}\") = " + "123123".matches("\\d{5,19}")); //true
        System.out.println("\"123\".matches(\"\\\\d{5,19}\") = " + "123".matches("\\d{5,19}")); //false

        // 4、其他几个常用的符号：(?i)忽略大小写 、 或：| 、  分组：()
        System.out.println("\"A\".matches(\"(?i)a\") = " + "A".matches("(?i)a")); //true
        System.out.println("\"a\".matches(\"[a|b|c]\") = " + "a".matches("[a|b|c]")); //true
        System.out.println("\"aabbcc\".matches(\"(\\\\w{2}){3}\") = " + "aabbcc".matches("(\\w{2}){3}")); //true
        System.out.println("\"abbcc\".matches(\"(\\\\w{2}){3}\") = " + "abbcc".matches("(\\w{2}){3}")); //false

        // 需求1：要求要么是3个小写字母，要么是3个数字。 [a-z]{3}|\d{3}
        System.out.println("\"abc\".matches(\"[a-z]{3}|\\\\d{3}\") = " + "abc".matches("[a-z]{3}|\\d{3}")); //true
        System.out.println("\"123\".matches(\"[a-z]{3}|\\\\d{3}\") = " + "123".matches("[a-z]{3}|\\d{3}")); //true
        System.out.println("\"a12\".matches(\"[a-z]{3}|\\\\d{3}]\") = " + "a12".matches("[a-z]{3}|\\d{3}]")); //false

        // 需求2：必须是”我爱“开头，中间可以是至少一个”编程“，最后至少是1个”666“   我爱(编程)+(666)
        String str = "我爱编程编程666666";
        System.out.println("str.matches(\"我爱(编程)+(666)+\") = " + str.matches("我爱(编程)+(666)+")); //true
        boolean matches = str.matches("我爱(编程){1,}(666){1,}");
        System.out.println("matches = " + matches);
    }
}
